/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs_modeler.wizards.listComputingObjects;

import java.awt.Component;
import java.awt.Dialog;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JComponent;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.ProjectUtils;
import org.openide.DialogDisplayer;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;

// An example action demonstrating how the wizard could be called from within
import pl.put.dcs_modeler.exceptions.dcsModelerIOException.DCSModelerObjectIOException;
import pl.put.dcs_modeler.exceptions.dcsModelerIOException.DCSModelerObjectIOExceptionComputingObjectNotFound;
import pl.put.dcs_modeler.utils.FileUtil;
// your code. You can copy-paste the code below wherever you need.
public final class ListComputingObjectsWizardAction
{

    private String computingObj;
    private String name;
    private WizardDescriptor.Panel[] panels;
    private static Logger log = Logger.getLogger( ListComputingObjectsWizardAction.class.getName() );
    private String pathToDataDecompositor;

    public ListComputingObjectsWizardAction( Project project ) throws DCSModelerObjectIOException
    {
        init( project );
    }

    public void init( Project project ) throws DCSModelerObjectIOException
    {
        String projectDir = project.getProjectDirectory().getPath();

        FileObject projectRsrc = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project/DCS/DCS Project" );
        String defaultProjectPkg = ( String )projectRsrc.getAttribute( "defaultProjectSrcPackage" );
        ProjectInformation projectInfo = ProjectUtils.getInformation( project );

        // pozyskanie ptrzebnych danych
        String projectName = projectInfo.getName();
        FileObject temp = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/DCS/newComputingObject" );
        String pathToCO = projectDir + "/src/" + defaultProjectPkg + "/" + projectName + "/" + ( String )temp.getAttribute( "defaultPackage" ) ;


        //String pathToCO = projectDir + "/src/resources/computingObjects";
        List<String> fileList = FileUtil.getDirsFileList( pathToCO );
        FileObject template;
        if ( fileList.size() == 0 )
        {
            //JOptionPane.showMessageDialog( null, "There aren't any computing objects in Your project." );
            throw new DCSModelerObjectIOExceptionComputingObjectNotFound( project.getProjectDirectory().getPath() + "/src/resources/computingObjects" );
        }
        else
        {
            WizardDescriptor wizardDescriptor = new WizardDescriptor( getPanels() );
            // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
            wizardDescriptor.setTitleFormat( new MessageFormat( "{0}" ) );
            wizardDescriptor.setTitle( "Select computing object" );



            wizardDescriptor.putProperty( "computingObjectsList", fileList );
            Dialog dialog = DialogDisplayer.getDefault().createDialog( wizardDescriptor );
            dialog.setVisible( true );
            dialog.toFront();
            boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
            if ( !cancelled )
            {
                try
                {
                    computingObj = ( String )wizardDescriptor.getProperty( "selectedCO" );
                    this.name = ( String )wizardDescriptor.getProperty( "coName" );
                    template = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/DCS/DataDecompositor.java" );
                    projectRsrc = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project/DCS/DCS Project" );
                    String defaultAgregatorPkg = ( String )template.getAttribute( "defaultPackage" );
                    defaultProjectPkg = ( String )projectRsrc.getAttribute( "defaultProjectSrcPackage" );
                    projectInfo = ProjectUtils.getInformation( project );

                    // pozyskanie ptrzebnych danych
                    // String projectName = projectInfo.getName();
                    String pathToAgregatorSrc = projectDir + "/src/" + defaultProjectPkg + "/" + projectName + "/" + defaultAgregatorPkg;
                    String coVisibleName = ( name.split( ".dcsco" ) )[ 0 ];
                    coVisibleName = coVisibleName.substring( 0, 1 ).toUpperCase() + coVisibleName.substring( 1 );
                    FileObject fo = org.openide.filesystems.FileUtil.createFolder( new File( pathToAgregatorSrc ) );
                    DataFolder df = DataFolder.findFolder( fo );
                    DataObject dTemplate = DataObject.find( template );
                    String pathToDataDecomp = coVisibleName + template.getName();
                    DataObject dobj = dTemplate.createFromTemplate( df, pathToDataDecomp );
                    setPathToDataDecompositor( pathToDataDecomp );
                }
                catch ( IOException ex )
                {
                    Exceptions.printStackTrace( ex );
                }

            }

        }
    }

    public String getSelectedComputingObject()
    {
        return this.computingObj;
    }

    /**
     * Initialize panels representing individual wizard's steps and sets
     * various properties for them influencing wizard appearance.
     */
    private WizardDescriptor.Panel[] getPanels()
    {
        if ( panels == null )
        {
            panels = new WizardDescriptor.Panel[]
                    {
                        new ListComputingObjectsWizardPanel1()
                    };
            String[] steps = new String[ panels.length ];
            for ( int i = 0; i < panels.length; i++ )
            {
                Component c = panels[i].getComponent();
                // Default step name to component name of panel. Mainly useful
                // for getting the name of the target chooser to appear in the
                // list of steps.
                steps[i] = c.getName();
                if ( c instanceof JComponent )
                { // assume Swing components
                    JComponent jc = ( JComponent )c;
                    // Sets step number of a component
                    // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
                    jc.putClientProperty( "WizardPanel_contentSelectedIndex", new Integer( i ) );
                    // Sets steps names for a panel
                    jc.putClientProperty( "WizardPanel_contentData", steps );
                    // Turn on subtitle creation on each step
                    jc.putClientProperty( "WizardPanel_autoWizardStyle", Boolean.TRUE );
                    // Show steps on the left side with the image on the background
                    jc.putClientProperty( "WizardPanel_contentDisplayed", Boolean.TRUE );
                    // Turn on numbering of all steps
                    jc.putClientProperty( "WizardPanel_contentNumbered", Boolean.TRUE );
                }
            }
        }
        return panels;
    }

    public String getName()
    {
        return this.name;
    }

    public String getPathToDataDecompositor()
    {
        return pathToDataDecompositor;
    }

    public void setPathToDataDecompositor( String pathToDataDecompositor )
    {
        this.pathToDataDecompositor = pathToDataDecompositor;
    }


}
